import citySelect from '../components/city-select/citySelect.js'

const getLetter = function (str) {
  return citySelect.getFirstLetter(str[0])
}
/**
 * @desc 获取城市首字母的unicode
 * @param letter String 城市索引
 */
const cityIndexPosition = function (letter) {
  if (!letter) {
    return ''
  }
  const ACode = 65
  return letter === '#' ? 26 : letter.charCodeAt(0) - ACode
}
export const initHotelCity = function (handleCity, cityindexs, hotelCitys) {
  const cityLetterArr = Object.keys(hotelCitys)
  cityindexs = cityindexs.concat(cityLetterArr)
  for (let index = 0; index < cityLetterArr.length; index++) {
    handleCity.push({
      name: cityLetterArr[index],
      isCity: true, // 用于区分是否含有当前字母开头的城市
      citys: hotelCitys[cityLetterArr[index]].map((item) => {
        item.cityName = item.name
        item.pinyin = item.spell
        item.unicode = getLetter(item.name).unicode
        return item
      }), // 存放城市首字母含是此字母的数组
      forName:
        'city-letter-' +
        (cityLetterArr[index] == '#' ? '0' : cityLetterArr[index]), //label的绑定
    })
  }
}

export const updateHotelCitys = function (data) {
  let keys = Object.keys(data)
  let cityData = []
  for (let index = 0; index < keys.length; index++) {
    cityData = cityData.concat(data[keys[index]])
  }
  return cityData
}

export const initializationCity = function () {
  let handleCity = []
  const cityLetterArr = [
    'A',
    'B',
    'C',
    'D',
    'E',
    'F',
    'G',
    'H',
    'I',
    'J',
    'K',
    'L',
    'M',
    'N',
    'O',
    'P',
    'Q',
    'R',
    'S',
    'T',
    'U',
    'V',
    'W',
    'X',
    'Y',
    'Z',
    '#',
  ]
  for (let index = 0; index < cityLetterArr.length; index++) {
    handleCity.push({
      name: cityLetterArr[index],
      isCity: false, // 用于区分是否含有当前字母开头的城市
      citys: [], // 存放城市首字母含是此字母的数组
      forName:
        'city-letter-' +
        (cityLetterArr[index] == '#' ? '0' : cityLetterArr[index]), //label的绑定
    })
  }
  return handleCity

}
export const buildCityindexs = function (cityindexs, cityData, formatName, handleCity) {
  for (let i = 0; i < cityData.length; i++) {
    // 获取首字母
    const cityLetter = getLetter(
      cityData[i][formatName]
    ).firstletter
    // 获取当前城市首字母的unicode，用作后续排序
    const unicode = getLetter(
      cityData[i][formatName]
    ).unicode
    const index = cityIndexPosition(cityLetter)
    if (cityindexs.indexOf(cityLetter) === -1) {
      handleCity[index].isCity = true
      cityindexs.push(cityLetter)
    }
    handleCity[index].citys.push({
      cityName: cityData[i][formatName],
      unicode: unicode,
      oldData: cityData[i],
    })
  }
}
